﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Library.Bll;
using Library.Common;

namespace HanhuaMicroCredit.BLL.File
{
    public static class FileBLL
    {
        private const string _TableName = "Set_FileUpload";

        public static List<Dictionary<string, object>> GetList(string isValid)
        {
            StringBuilder sql = new StringBuilder();
            if (!string.IsNullOrEmpty(isValid))
            {
                sql.AppendFormat(" AND IsValid={0}", isValid);
            }
            if (sql.Length > 0)
            {
                sql.Remove(0, 5);
            }

            return CommonBLL.GetList(_TableName, "Id,No,Name,Descriptions,SavePath,MaxSize,MaxCount,FileTypes,IsValid", "Id", sql.ToString());
        }

        public static Dictionary<string, object> GetModel(int id)
        {
            return CommonBLL.GetModel(_TableName, "Id,No,Name,Descriptions,SavePath,MaxSize,MaxCount,FileTypes,IsImageServer,ThumbnailName,ThumbnailWidth,ThumbnailHeight,ThumbnailMode,WatermarkWordsName,WatermarkWords,WatermarkImageName,WatermarkImagePath,ReturnFileName", id);
        }

        public static string AddOrUpdate(int id, Dictionary<string, object> data)
        { 
            List<ConditionInfo> cs = new List<ConditionInfo>();
            cs.Add(new ConditionInfo("No", CompareType.Equals, data["No"]));
            if(id > 0)
            {
                cs.Add(new ConditionInfo("Id", CompareType.NotEquals, id.ToString()));
            }

            if (CommonBLL.Exists(_TableName, cs))
            {
                return "该编码已经存在！";
            }

            if (id > 0)
            {
                //修改
                data.Add("Id", id.ToString());
                CommonBLL.Update(_TableName, data, new string[] { "Id" });
            }
            else
            {
                //新增
                CommonBLL.Add(_TableName, data);
            }

            return null;
        }
    }
}
